package com.snow.useviewdemo.num;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.snow.common.tool.utils.AppUtils;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.animation.LiveRightView;

import static android.view.View.VISIBLE;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-09-27
 * desc   :
 */
public class NUMScaleActivity extends AppCompatActivity {
    TextView countNumTv;
    LiveRightView liveRightView;
    private int countNum;
    private AnimatorSet animSet;
    private RunNumTextView runNumTextView;
    private EditText input_et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_num_scale);
        countNumTv = findViewById(R.id.count_num_tv);
        liveRightView = findViewById(R.id.lcdv_view);
        runNumTextView = findViewById(R.id.runNumTextView);
        input_et = findViewById(R.id.input_et);
        initListener();
        initCountDownTimeAnimation();
        runNumTextView.setDecimalFormat("0.0", 2000);
    }

    private void initListener() {
        findViewById(R.id.btn_01).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countNum = 10;
                countDownNum();
            }
        });
        findViewById(R.id.btn_02).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = String.valueOf(input_et.getText());
                if (!TextUtils.isEmpty(num)) {
                    runNumTextView.setMoney(Float.parseFloat(num));
                }
            }
        });
    }

    /**
     * //倒计时的动画
     */
    private void initCountDownTimeAnimation() {
        ObjectAnimator rotateX = ObjectAnimator.ofFloat(countNumTv, "scaleX", 1f, 4f, 1f);
        ObjectAnimator rotateY = ObjectAnimator.ofFloat(countNumTv, "scaleY", 1f, 4f, 1f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(countNumTv, "alpha", 0f, 1f, 0f);
        animSet = new AnimatorSet();
        animSet.play(rotateX).with(rotateY).with(alpha);
        animSet.setDuration(1000);
        animSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                countNum--;
                if (countNum == 0) {
                    liveRightView.setVisibility(VISIBLE);
                    liveRightView.startAnimator();
                } else {
                    countDownNum();
                }
            }
        });
    }

    /**
     * 连麦倒计时
     */
    public void countDownNum() {
        if (countNum < 0) {
            return;
        }
        countNumTv.setText("" + countNum);
        initCountDownTimeAnimation();
        animSet.start();
    }



    /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {  //把操作放在用户点击的时候
            View v = getCurrentFocus();      //得到当前页面的焦点,ps:有输入框的页面焦点一般会被输入框占据
            if (isShouldHideKeyboard(v, me)) { //判断用户点击的是否是输入框以外的区域
                AppUtils.init().closeInputMethod(this);   //收起键盘
            }
        }
        return super.dispatchTouchEvent(me);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {  //判断得到的焦点控件是否包含EditText
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],    //得到输入框在屏幕中上下左右的位置
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击位置如果是EditText的区域，忽略它，不收起键盘。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略
        return false;
    }
}
